package EveryDay;
/**
 * 14. 最长公共前缀
 * */
public class P14 {
    public static void main(String[] args) {
        String[] s={"flower","flow","flight"};

        P14Test01 p14Test01=new P14Test01();
        System.out.println(p14Test01.longestCommonPrefix(s));
    }
}

class P14Test01{
    public String longestCommonPrefix(String[] strs) {

        if(strs[0].isEmpty()){
            return "";
        }
        int length=strs.length;
        int len=Integer.MAX_VALUE;
        int minIndex=0;
        for(int i=0;i<length;i++){
            if(len>strs[i].length()){
                minIndex=i;
                len=strs[i].length();
            }
        }
        String ans="";
        for(int i=len;i>=1;i--){
            String s=strs[minIndex].substring(0, i);
            int flag=0;
            for(int j=0;j<length;j++){
                if(!s.equals(strs[j].substring(0, i))){
                    flag=-1;
                    break;
                }
            }
            if(flag==0){
                ans=s;
                break;
            }
        }
        return ans;
    }
}
